import { color } from "echarts";
import ApiClient from "../../axios/index";
interface IData{
    name:string,
    value:number
}
const apiClient=new ApiClient();

export const getData=(getOption?:(data:any)=>void)=>{
return apiClient.get<IData[]>('seller').then((res)=>{
    const pageTotal=Math.ceil(res.data!.length/5);
    const data=uodateTime(pageTotal,res.data!,(data:IData[])=>{
        const sliceData= data.sort((a,b)=>a.value-b.value);
       const name=sliceData.map((item)=>item.name);
       const value=sliceData.map((item)=>item.value);
       const optionData= {
        title:{
            text:'商家销售统计',
            textStyle:{
                fontSize:20,
            },
            left:'50%',
        },
           xAxis:{
               type:'value'
           },
           yAxis:{
               type:'category',
               data:name
           },
           tooltip:{
               trigger:'axis',
               axisPointer:{
                   type:'line',
                   z:0,
                   lineStyle:{
                    width:66,
                       color:'#2D3443'
                   }
               }
           },
           series:[
               {
                   type:'bar',
                   data:value,
                   barWidth:30,
                   label:{
                       show:true,
                       position:'right',
                       color:'white',
                       fontSize:16,
                     
                   },
                   itemStyle:{
                       borderRadius:10,
                   }
               }
           ]
       }
       getOption&&getOption(optionData);
    });
    return data;
  })
}
const getUpdateData=(currentPage=1,value?:IData[])=>{
    const data=[...value!];
    const start=(currentPage-1)*5;
    const end=currentPage*5;
    const currentData=data.slice(start,end);
    return currentData
}

// 每三秒更新一下
const uodateTime=(pageTotal:number,value?:IData[],onUpdate?: (updateData: IData[]) => void)=>{
    let currentPage=1;
    const data=[...value!];
    const updateData= getUpdateData(currentPage,data);
    updateData&&onUpdate!(updateData)
    const timer=setInterval(()=>{
        if(currentPage>=pageTotal){
            currentPage=1;
        }
        currentPage++;
        const updateData= getUpdateData(currentPage,data);
        updateData&&onUpdate!(updateData)
    },3000)
    return {
        timer,
    }
}